<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends Admin_Controller {
	private $conn;
	
	public function __construct(){  
        parent::__construct();
		$this->conn = $this->load->database('default',true);
    }

    public function test(){
    	$this->load->library('Cny_Class');
    	$this->cny_class->flushUserCache(100078);
    }

    public function ipCount(){
    	$log_conn = $this->load->database('log',true);
		for($i=0;$i<10;$i++){
			$table = splitTable('cfd_log_login', $i, 10);
			$sql = "select uid,ip from $table";
			$query = $log_conn->query($sql);
    		$tmpLogin[$i] = $query->result_array();
		}
		$ipArr = array();
		$tmp = array();
		foreach($tmpLogin as $items){
			foreach($items as $item){
				$ipArr[$item['ip']] = isset($ipArr[$item['ip']])?bcadd($ipArr[$item['ip']],1):1;
				$tmp[$item['ip']][$item['uid']] = isset($tmp[$item['ip']][$item['uid']])?bcadd($tmp[$item['ip']][$item['uid']],1):1;
			}
		}
		array_multisort($ipArr);
		echo "<pre>";
		
		$data = array();
		foreach($ipArr as $k=>$v){
			if($v>30){
				$data[] = array(
					'ip' => $k,
					'num' => $v,
					'uids' => $tmp[$k]
				);
			}
		}
		// var_dump($data);
		foreach($data as $items){
			echo $items['ip']. " 出现次数：".$items['num']." 账号数：".count($items['uids']);
			echo "<br>";
		}
    }

    public function updateChiPrice(){
    	$tradeLogTb = 'cfd_cfd_trade_log';
    	$askTb = 'cfd_cfd_ask';
    	$bidTb = 'cfd_cfd_bid';
    	$posTb = 'cfd_cfd_position';

    	$sql = "select id,bs,trade_id,source from $tradeLogTb where kp='close' and chi_price=0 order by id asc";
    	$query = $this->conn->query($sql);
    	$tradeLogArr = $query->result_array();

    	$sql = "select id,posid from $askTb where kp='close' order by id asc";
    	$query = $this->conn->query($sql);
    	$tmpAskArr = $query->result_array();
    	$askArr = array();
    	foreach($tmpAskArr as $item){
    		$askArr[$item['id']] = $item['posid'];
    	}

    	$sql = "select id,posid from $bidTb where kp='close' order by id asc";
    	$query = $this->conn->query($sql);
    	$tmpBidArr = $query->result_array();
    	$bidArr = array();
    	foreach($tmpBidArr as $item){
    		$bidArr[$item['id']] = $item['posid'];
    	}

    	$sql = "select id,price from $posTb order by id asc";
    	$query = $this->conn->query($sql);
    	$tmpPosArr = $query->result_array();
    	$posArr = array();
    	foreach($tmpPosArr as $item){
    		$posArr[$item['id']] = $item['price'];
    	}
    	$upArr = array();
    	foreach($tradeLogArr as $item){
    		if(in_array($item['source'],array(10004,50004))){//开仓
    			if($item['bs']=='bid'){
    				$upArr[$item['id']] = $posArr[$bidArr[$item['trade_id']]];
    			}else{
	    			$upArr[$item['id']] = $posArr[$askArr[$item['trade_id']]];
	    		}
    		}else{//结算、爆仓
    			$upArr[$item['id']] = $posArr[$item['trade_id']];
    		}
    	}
    	// echo "<pre>";
    	// print_r($upArr);
    	foreach($upArr as $tradeid=>$chi_price){
    		$sql = "update $tradeLogTb set chi_price=$chi_price where id=$tradeid";
    		echo $sql."\n";
    		// $this->conn->query($sql);
    	}
    	echo "完成";

    }

	public function index()
	{	
		//30分钟内有操作的人数
		$min30  = time()-30*60;
		$sql	= "select count(*) total from cfd_online where lastest_update>='$min30' and platform='".$this->platform."'";
		$query	= $this->conn->query($sql);
		$rows	= $query->row_array();
		$this->data['online']=$rows['total'];

		$now_date	=	date('Y-m-d');
		$sql		=	"select count(*) total from cfd_user_info where create_time>='$now_date' and platform='".$this->platform."' ";
		$query		=	$this->conn->query($sql);
		$rows		=	$query->row_array();
		$this->data['new_user']=$rows['total'];
	    
	    $server			=	array();
		$page_arr=array(
				"url"=>"/index.php",
				"msg"=>"该页面不存在",
				"time"=>3
		);
	    $server['time']	=	date("Y-m-d h:i:s",$_SERVER['REQUEST_TIME']);
	    $server['port']	=	$_SERVER['SERVER_PORT'];
	    $server['os']	=	@PHP_OS;
	    $server['version']	=	@PHP_VERSION;
	    $server['name']		=	$_SERVER['SERVER_NAME'];
	    $server['software']	=	$_SERVER['SERVER_SOFTWARE'];
	    $server['root']		=	$_SERVER['DOCUMENT_ROOT'];
	    $server['upload']	=	@ini_get('upload_max_filesize');
	    $session_timeout	=	@ini_get('session.gc_maxlifetime');
	    $server['timeout']	=	$session_timeout?$session_timeout/60:'未知';
	    $server['register_globals']	=	$this->get_php_config('register_globals');
	    $server['safe_mode']		=	$this->get_php_config('safe_mode');
	    $server['memory_usage']		=	$this->format_size(memory_get_usage());
	    $server['disable_functions']=	@ini_get('disable_functions');
	    $this->data['server']	=	$server;
		$this->data['page']		=	'welcome_index';
		$this->data['page_data']=	$page_arr;
	    $this->load->view('layout',$this->data);
	}

	public function chiYK(){
		echo "持仓盈亏：<br>";
		$this->load->library('Report_Class');
		$chi_data = $this->report_class->getCfdChiInfo();
		print_r($chi_data);
		$zyk = 0;
		foreach($chi_data as $item){
			$zyk = bcadd($item['yk'],$zyk,2);
		}
		echo "<br>";
		echo "总盈亏：".$zyk;
	}
	
	private function get_php_config($name){
		switch($result=@get_cfg_var($name)){
			case 0:
				return '×';
				break;
			case 1:
				return '√';
				break;
			default:
				return $result;
				break;
		}
	}

	private function format_size($filesize){
		if($filesize >= 1073741824){
			$filesize=round($filesize / 1073741824 * 100) / 100 . ' GB';
		}elseif ($filesize >= 1048576){
			$filesize=round($filesize / 1048576 * 100) / 100 . ' MB';
		}elseif($filesize >= 1024){
			$filesize=round($filesize / 1024 * 100) / 100 . ' KB';
		}else{
			$filesize=$filesize.' Bytes';
		}
		return $filesize;
	}
}
?>